<?php
@Session_start();
defined('G_IN_SYSTEM')or exit('No permission resources.');
System::load_app_class('base');
class user extends base {
	public function __construct() {
		parent::__construct();
		$this->db=System::load_sys_class('model');
		
	}
	public function requestPublicKey() {
		$publicKey = $this->rsa->getPublicKey();
		$info = array("publicKey" => $publicKey,"open" => 1);
		$this->success($info);

	}
	//根据token 获取用户信息
	public function getUserInfo($logintoken){
		$member=$this->db->GetOne("select * from `@#_member` where `logintoken`='$logintoken'");
		return $member;
	}
	//根据手机号获取用户信息
	public  function  getUserInfoWihtMobile($mobile){
		$member=$this->db->GetOne("select * from `@#_member` where `mobile`='$mobile' and `mobilecode`=1");
		return $member;
	}
	//根据用户id获取用户信息
	public  function  getUserInfoWithUid($uid){
		$member = $this->db->GetOne("SELECT * FROM `@#_member` WHERE `uid` = '$uid'");
		return $member;
	}
	//登录
	public function login(){
			$username=$_POST['username'];
			$password=$this->rsa->decrypt($_POST['password']);
			$logintoken=$this->rsa->decrypt($_POST['logintoken']);
			
			if($logintoken){
			$member=$this->db->GetOne("select * from `@#_member` where `logintoken`='$logintoken'");
			if(!$member){
				$this->failed("验证已过期，请重新登录");
			}else{
				$ip  = _get_ip_dizhi();
				$time = time();
				$token = md5($logintoken.time());
				$member["token"] = $this->rsa->encrypt($token);
				$logintoken = $this->rsa->encrypt($logintoken);
				$member['logintoken'] = $logintoken;
				$member['requestWithoutSessino'] = "true";
				_setcookie("token",$token,3600);
				_setcookie("logintoken",$logintoken,3600);
				$_SESSION['token'] = $token;
				$cost = $this->db->GetOne("SELECT SUM(`moneycount`) AS `cost` from `@#_member_go_record` where `uid`=".$member['uid']."");
				$member['cost'] = $cost['cost'];
				$charge = $this->db->GetOne("SELECT SUM(`money`) as `money` FROM `@#_member_addmoney_record` WHERE `uid` = ".$member['uid']." AND `status` = '已付款'");
				$member['charge'] = $charge['money'];
				$ok = $this->db->Query("UPDATE `@#_member` SET `user_ip`='$ip',`login_time`='$time' WHERE `uid`=".$member['uid']."");
				$this->success($member);
			}
			}else if($username){
			
			$logintype='mobile';
			if(strpos($username,'@')==false){
				//手机
				$logintype='mobile';
				if(!_checkmobile($username)){
					$this->failed("手机格式不正确!");
				}
			}else{
				//邮箱
				$logintype='email';
				if(!_checkemail($username)){
					$this->failed("邮箱格式不正确!");
				}
			}
			$member=$this->db->GetOne("select * from `@#_member` where `$logintype`='$username' and `password`='$password'");
			//var_dump($member);
			if(!$member){
				$this->failed("帐号或密码错误!");
			}else{
				$ip  = _get_ip_dizhi();
				$time = time();
				$logintoken = md5(time().$password);
				$ok = $this->db->Query("UPDATE `@#_member` SET `logintoken`='$logintoken' , `user_ip`='$ip',`login_time`='$time' WHERE `$logintype`='$username' and `password`='$password'");
				if($ok){
					$member["logintoken"] = $this->rsa->encrypt($logintoken);
					$member["token"] = $this->rsa->encrypt($logintoken);
					_setcookie("token",$logintoken,3600);
					_setcookie("logintoken",$logintoken,3600);
				}
				$cost = $this->db->GetOne("SELECT SUM(`moneycount`) AS `cost` from `@#_member_go_record` where `uid`=".$member['uid']."");
				$member['cost'] = $cost['cost'];
				$charge = $this->db->GetOne("SELECT SUM(`money`) as `money`  FROM `@#_member_addmoney_record` WHERE `uid` = ".$member['uid']." AND `status` = '已付款'");
				$member['charge'] = $charge['money'];
				$this->success($member);
			}
		}else{
				$this->failed("验证已过期,请重新登录");
			}
	}

	public function wechatLogin()
{
	$jsondecode = $_POST['callBackInfo'];
	$wx_openid =$jsondecode["unionid"];
	if(!$wx_openid){
		_messagemobile("绑定失败");
	}

	$nickname = $jsondecode["nickname"];
	$nickname =mysql_real_escape_string($nickname);
	$unionid =$jsondecode["unionid"];
	$touimg = $jsondecode["headimgurl"];
	$go_user_info = $this->db->GetOne("select * from `@#_member_band` where `b_code` = '$wx_openid' and `b_type` = 'weixin' LIMIT 1");
	if(!$go_user_info){
		$userpass = md5("123456");
		$path = 'data/uploads/wximg/';
		$mulu = time();
		if($touimg){
			//$post_arr= array("img"=>$touimg,"path"=>$path,"mulu"=>$mulu);
			file_put_contents($path.$mulu.".jpg",file_get_contents($touimg));
			//$this->wximg($post_arr);
			_g_triggerRequest(WEB_PATH.'/mobile/user/wximg',false,$post_arr);
		}
		$go_user_img  = 'wximg/'.$mulu . '.jpg';
		$go_user_time = time();
		$ip = _get_ip_dizhi();
		$logintoken = md5(time().$userpass);
//$this->success("INSERT INTO `@#_member` (`username`,`password`,`img`,`money`,`band`,`time`,logintoken`) VALUES ('$nickname','$userpass','$go_user_img',0,'weixin','$go_user_time','$logintoken')");
		$q1 = $this->db->Query("INSERT INTO `@#_member` (`username`,`password`,`img`,`user_ip`,`login_time`,`money`,`band`,`time`,`logintoken`) VALUES ('$nickname','$userpass','$go_user_img','$ip','$go_user_time',0,'weixin','$go_user_time','$logintoken')");

		$uid = $this->db->insert_id();

		$go_user_time = time();
		$this->db->Query("INSERT INTO `@#_member_band` (`b_uid`, `b_type`, `b_code`, `b_time`) VALUES ('$uid', 'weixin', '$wx_openid', '$go_user_time')");
		$member = $this->db->GetOne("select * from `@#_member` where `uid` = '$uid' LIMIT 1");
		if($member){
			$member["logintoken"] = $this->rsa->encrypt($logintoken);
			$member["token"] = $this->rsa->encrypt($logintoken);
			_setcookie("token",$logintoken,3600);
			_setcookie("logintoken",$logintoken,3600);
			$this->success($member);
		}else{
			$this->failed("登录失败");
		}
	}else{
		$uid = $go_user_info["b_uid"];
		$member = $this->db->GetOne("select * from `@#_member` where `uid` = '$uid' LIMIT 1");

			if(empty($member)){
				$this->db->Query("delete from `@#_member_band` where `b_id` = '".$go_user_info['b_id']."'");
				$userpass = md5('123456');
				$path = 'data/uploads/wximg/';
				$mulu = time();
				if($touimg){
					$post_arr= array("img"=>$touimg,"path"=>$path,"mulu"=>$mulu);
					file_put_contents($path.$mulu.".jpg",file_get_contents($touimg));
					_g_triggerRequest(WEB_PATH.'/mobile/user/wximg',false,$post_arr);
				}
				$go_user_img  = 'wximg/'.$mulu . '.jpg';
				$go_user_time = time();
				$ip = _get_ip_dizhi();
				$logintoken = md5(time().$userpass);
				$q1 = $this->db->Query("INSERT INTO `@#_member` (`username`,`password`,`img`,`user_ip`,`login_time`,`money`,`band`,time`,`logintoken`) VALUES ('$nickname','$userpass','$go_user_img','$ip','$go_user_time',0,'weixin','$go_user_time','$logintoken')");
				$uid = $this->db->insert_id();
				$this->db->Query("INSERT INTO `@#_member_band` (`b_uid`, `b_type`, `b_code`, `b_time`) VALUES ('$uid', 'weixin', '$wx_openid', '$go_user_time')");
				$member = $this->db->GetOne("select * from `@#_member` where `uid` = '$uid' LIMIT 1");
				$member["logintoken"] = $this->rsa->encrypt($logintoken);
				$member["token"] = $this->rsa->encrypt(md5($logintoken.time()));
				_setcookie("token",$logintoken,3600);
				_setcookie("logintoken",$logintoken,3600);
			}else{
				$go_user_time = time();
				$ip = _get_ip_dizhi();
				$logintoken = md5(time().$member['password']);
				$ok = $this->db->Query("UPDATE `@#_member` SET `logintoken`='$logintoken',`user_ip`='$ip',`login_time`='$go_user_time' WHERE `uid`='$uid'");
				if($ok){

					$member["logintoken"] = $this->rsa->encrypt($logintoken);
					$member["token"] = $this->rsa->encrypt(md5($logintoken.time()));
					_setcookie("token",$logintoken,3600);
					_setcookie("logintoken",$logintoken,3600);
					if(!$member['mobile']){
						$member['noMobile'] = 1;
						$this->success($member);
					}
					$this->success($member);
				}else{
					$this->failed("token失效");
				}
			}
			$this->success($member);
		}
}

	public function qqLogin()
	{
		$jsondecode = $_POST['callBackInfo'];
		$wx_openid =$jsondecode["openid"];
		if(!$wx_openid){
			_messagemobile("绑定失败");
		}

		$nickname = $jsondecode["nickname"];
		$nickname =mysql_real_escape_string($nickname);
//		$unionid =$jsondecode["unionid"];
		$touimg = $jsondecode["headimgurl"];
		$go_user_info = $this->db->GetOne("select * from `@#_member_band` where `b_code` = '$wx_openid' and `b_type` = 'qq' LIMIT 1");
		if(!$go_user_info){
			$userpass = md5("123456");
			$path = 'data/uploads/wximg/';
			$mulu = time();
			if($touimg){
				//$post_arr= array("img"=>$touimg,"path"=>$path,"mulu"=>$mulu);
				file_put_contents($path.$mulu.".jpg",file_get_contents($touimg));
				//$this->wximg($post_arr);
				_g_triggerRequest(WEB_PATH.'/mobile/user/wximg',false,$post_arr);
			}
			$go_user_img  = 'wximg/'.$mulu . '.jpg';
			$go_user_time = time();
			$ip = _get_ip_dizhi();
			$logintoken = md5(time().$userpass);
//$this->success("INSERT INTO `@#_member` (`username`,`password`,`img`,`money`,`band`,`time`,logintoken`) VALUES ('$nickname','$userpass','$go_user_img',0,'weixin','$go_user_time','$logintoken')");
			$q1 = $this->db->Query("INSERT INTO `@#_member` (`username`,`password`,`img`,`user_ip`,`login_time`,`money`,`band_q`,`time`,`logintoken`) VALUES ('$nickname','$userpass','$go_user_img','$ip','$go_user_time',0,'qq','$go_user_time','$logintoken')");

			$uid = $this->db->insert_id();

			$go_user_time = time();
			$this->db->Query("INSERT INTO `@#_member_band` (`b_uid`, `b_type`, `b_code`, `b_time`) VALUES ('$uid', 'qq', '$wx_openid', '$go_user_time')");
			$member = $this->db->GetOne("select * from `@#_member` where `uid` = '$uid' LIMIT 1");
			if($member){
				$member["logintoken"] = $this->rsa->encrypt($logintoken);
				$member["token"] = $this->rsa->encrypt($logintoken);
				_setcookie("token",$logintoken,3600);
				_setcookie("logintoken",$logintoken,3600);
				$this->success($member);
			}else{
				$this->failed("登录失败");
			}
		}else{
			$uid = $go_user_info["b_uid"];
			$member = $this->db->GetOne("select * from `@#_member` where `uid` = '$uid' LIMIT 1");

			if(empty($member)){
				$this->db->Query("delete from `@#_member_band` where `b_id` = '".$go_user_info['b_id']."'");
				$userpass = md5('123456');
				$path = 'data/uploads/wximg/';
				$mulu = time();
				if($touimg){
					$post_arr= array("img"=>$touimg,"path"=>$path,"mulu"=>$mulu);
					file_put_contents($path.$mulu.".jpg",file_get_contents($touimg));
					_g_triggerRequest(WEB_PATH.'/mobile/user/wximg',false,$post_arr);
				}
				$go_user_img  = 'wximg/'.$mulu . '.jpg';
				$go_user_time = time();
				$logintoken = md5(time().$userpass);
				$go_user_time = time();
				$ip = _get_ip_dizhi();
				$q1 = $this->db->Query("INSERT INTO `@#_member` (`username`,`password`,`img`,`user_ip`,`login_time`,`money`,`band_q`,time`,`logintoken`) VALUES ('$nickname','$userpass','$go_user_img','$ip','$go_user_time',0,'qq','$go_user_time','$logintoken')");
				$uid = $this->db->insert_id();
				$this->db->Query("INSERT INTO `@#_member_band` (`b_uid`, `b_type`, `b_code`, `b_time`) VALUES ('$uid', 'qq', '$wx_openid', '$go_user_time')");
				$member = $this->db->GetOne("select * from `@#_member` where `uid` = '$uid' LIMIT 1");
				$member["logintoken"] = $this->rsa->encrypt($logintoken);
				$member["token"] = $this->rsa->encrypt(md5($logintoken.time()));
				_setcookie("token",$logintoken,3600);
				_setcookie("logintoken",$logintoken,3600);
			}else{
				$logintoken = md5(time().$member['password']);
				$go_user_time = time();
				$ip = _get_ip_dizhi();
				$ok = $this->db->Query("UPDATE `@#_member` SET `logintoken`='$logintoken',`user_ip`='$ip',`login_time`='$go_user_time' WHERE `uid`='$uid'");
				if($ok){
					$member["logintoken"] = $this->rsa->encrypt($logintoken);
					$member["token"] = $this->rsa->encrypt(md5($logintoken.time()));
					_setcookie("token",$logintoken,3600);
					_setcookie("logintoken",$logintoken,3600);
					if(!$member['mobile']){
						$member['noMobile'] = 1;
						$this->success($member);
					}
					$this->success($member);
				}else{
					$this->failed("token失效");
				}
			}
			$this->success($member);
		}
	}
	public function mobileBandWeChat(){
		$jsondecode = $_POST['callBackInfo'];
		$mobile = $_POST["mobile"];
		$wx_openid =$jsondecode["unionid"];
		$useWeChatInfo = $_POST['useWeChatInfo'];
		if(!$wx_openid){
			$this->failed("绑定失败");
		}
		if($mobile) {
			$member = $this->getUserInfoWihtMobile($mobile);
			if($member) {
//				$unionid = $jsondecode["unionid"];
				$touimg = $useWeChatInfo ? $jsondecode["headimgurl"] : 0;

				$go_user_info = $this->db->GetOne("select * from `@#_member_band` where `b_code` = '$wx_openid' and `b_type` = 'weixin' LIMIT 1");
				$path = 'data/uploads/wximg/';
				$mulu = time();

				if ($touimg) {
					file_put_contents($path . $mulu . ".jpg", file_get_contents($touimg));
					_g_triggerRequest(WEB_PATH . '/mobile/user/wximg', false, $post_arr);
				}
				$go_user_img = 'wximg/' . $mulu . '.jpg';
				$image = $useWeChatInfo ? $go_user_img : $member['img'];
				$userName = $useWeChatInfo ? $jsondecode['nickname'] : $member['username'];
				$go_user_time = time();
				$uid = $member['uid'];
				if (!$go_user_info) {
					$q1 = $this->db->Query("UPDATE `@#_member` set `username` = '$userName', `band` = 'weixin', `img` = '$image',`time` = '$go_user_time' WHERE `uid` = $uid");
					$go_user_time = time();
					$this->db->Query("INSERT INTO `@#_member_band` (`b_uid`, `b_type`, `b_code`, `b_time`) VALUES ('$uid', 'weixin', '$wx_openid', '$go_user_time')");
					$member = $this->db->GetOne("select * from `@#_member` where `uid` = '$uid' LIMIT 1");
					$member['token'] = $this->rsa->encrypt(md5($member['logintoken'].time()));
					$member['logintoken'] = $this->rsa->encrypt($member['logintoken']);
					_setcookie("token",$member['token'],3600);
					_setcookie("logintoken",$member['logintoken'],3600);
					if ($member["band"]) {
						$this->success($member);
					} else {
						$this->failed("绑定失败");
					}
				} else {
					$bandMember = $this->getUserInfoWithUid($go_user_info['b_uid']);
					if ($bandMember['mobile'] != $mobile && $bandMember['mobile']) {
						$this->failed('该微信号已绑定其他号码');
					} else {
						$money = $member['money'] + $bandMember['money'];
						$yaoqing = $bandMember['time'] > $member['time'] ? $member['yaoqing'] : $bandMember['yaoqing'];
						$sql1 = "UPDATE `@#_member` set `username` = '$userName' , `band`='weixin',`img`= '$image',`time`='$go_user_time',`money`= " . $money . ",`yaoqing`='$yaoqing' WHERE `uid` = '" . $uid. "'";
						$q1 = $this->db->Query($sql1);
						$q3 = $this->db->Query("UPDATE `@#_member_band` set `b_uid` = '$uid' WHERE `b_code` = '$wx_openid' and `b_type` = 'weixin'");
						$q4 = $this->db->Query("UPDATE `@#_member_account` set `uid` = '$uid' WHERE `uid` = ".$bandMember['uid']."");
						$q5 = $this->db->Query("UPDATE `@#_member_addmoney_record` set `uid` = '$uid' WHERE `uid` = ".$bandMember['uid']."");
						$q6 = $this->db->Query("UPDATE `@#_member_band` set `uid` = '$uid' WHERE  `uid` = ".$bandMember['uid']."");
						$q7 = $this->db->Query("UPDATE `@#_member_op_record` set `uid` = '$uid' WHERE  `uid` = ".$bandMember['uid']."");
						$member = $this->getUserInfoWihtMobile($mobile);
						$member['token'] = $this->rsa->encrypt(md5($member['logintoken'].time()));
						$member['logintoken'] = $this->rsa->encrypt($member['logintoken']);
						_setcookie("token",$member['token'],3600);
						_setcookie("logintoken",$member['logintoken'],3600);
						if ($q1 && $q3) {
							$this->success($member);
						} else {
							$this->failed('绑定失败');
						}
					}
				}
			}else{
				$this->failed("无此用户");
			}
		}else{
			$this->failed("手机号码错误:".$mobile);
		}
	}


	public function mobileBandQQ(){
		$jsondecode = $_POST['callBackInfo'];
		$mobile = $_POST["mobile"];
		$wx_openid =$jsondecode["openid"];
		$useWeChatInfo = $_POST['useWeChatInfo'];
		if(!$wx_openid){
			$this->failed("绑定失败");
		}
		if($mobile) {
			$member = $this->getUserInfoWihtMobile($mobile);
			if($member) {
				$touimg = $useWeChatInfo ? $jsondecode["headimgurl"] : 0;

				$go_user_info = $this->db->GetOne("select * from `@#_member_band` where `b_code` = '$wx_openid' and `b_type` = 'qq' LIMIT 1");
				$path = 'data/uploads/wximg/';
				$mulu = time();

				if ($touimg) {
					file_put_contents($path . $mulu . ".jpg", file_get_contents($touimg));
					_g_triggerRequest(WEB_PATH . '/mobile/user/wximg', false, $post_arr);
				}
				$go_user_img = 'wximg/' . $mulu . '.jpg';
				$image = $useWeChatInfo ? $go_user_img : $member['img'];
				$userName = $useWeChatInfo ? $jsondecode['nickname'] : $member['username'];
				$go_user_time = time();
				$uid = $member['uid'];
				if (!$go_user_info) {
					$q1 = $this->db->Query("UPDATE `@#_member` set `username` = '$userName', `band_q` = 'qq', `img` = '$image',`time` = '$go_user_time' WHERE `uid` = $uid");
					$go_user_time = time();
					$this->db->Query("INSERT INTO `@#_member_band` (`b_uid`, `b_type`, `b_code`, `b_time`) VALUES ('$uid', 'qq', '$wx_openid', '$go_user_time')");
					$member = $this->db->GetOne("select * from `@#_member` where `uid` = '$uid' LIMIT 1");
					$member['token'] = $this->rsa->encrypt(md5($member['logintoken'].time()));
					$member['logintoken'] = $this->rsa->encrypt($member['logintoken']);
					_setcookie("token",$member['token'],3600);
					_setcookie("logintoken",$member['logintoken'],3600);
					if ($member["band"]) {
						$this->success($member);
					} else {
						$this->failed("绑定失败");
					}
				} else {
					$bandMember = $this->getUserInfoWithUid($go_user_info['b_uid']);
					if ($bandMember['mobile'] != $mobile && $bandMember['mobile']) {
						$this->failed('该QQ号已绑定其他号码');
					} else {
						$money = $member['money'] + $bandMember['money'];
						$yaoqing = $bandMember['time'] > $member['time'] ? $member['yaoqing'] : $bandMember['yaoqing'];
						$sql1 = "UPDATE `@#_member` set `username` = '$userName' , `band_q`='qq',`img`= '$image',`time`='$go_user_time',`money`= " . $money . " ,`yaoqing`='$yaoqing' WHERE `uid` = '" . $uid. "'";
						$q1 = $this->db->Query($sql1);
						$q3 = $this->db->Query("UPDATE `@#_member_band` set `b_uid` = '$uid' WHERE `b_code` = '$wx_openid' and `b_type` = 'qq'");
						$q4 = $this->db->Query("UPDATE `@#_member_account` set `uid` = '$uid' WHERE `uid` = ".$bandMember['uid']."");
						$q5 = $this->db->Query("UPDATE `@#_member_addmoney_record` set `uid` = '$uid' WHERE `uid` = ".$bandMember['uid']."");
						$q6 = $this->db->Query("UPDATE `@#_member_band` set `uid` = '$uid' WHERE  `uid` = ".$bandMember['uid']."");
						$q6 = $this->db->Query("UPDATE `@#_member_go_record` set `uid` = '$uid' WHERE  `uid` = ".$bandMember['uid']."");
						$q6 = $this->db->Query("UPDATE `@#_member_op_record` set `uid` = '$uid' WHERE  `uid` = ".$bandMember['uid']."");
						$member = $this->getUserInfoWihtMobile($mobile);
						$member['token'] = $this->rsa->encrypt(md5($member['logintoken'].time()));
						$member['logintoken'] = $this->rsa->encrypt($member['logintoken']);
						_setcookie("token",$member['token'],3600);
						_setcookie("logintoken",$member['logintoken'],3600);
						if ($q1 && $q3) {
							$this->success($member);
						} else {
							$this->failed('绑定失败');
						}
					}
				}
			}else{
				$this->failed("无此用户");
			}
		}else{
			$this->failed("手机号码错误:".$mobile);
		}
	}


	public function getRegisterCode(){
		$mobile = $_POST['mobile'];
		if($mobile) {
			$member = $this->getUserInfoWihtMobile($mobile);
			if($member) {
				$this->failed("手机号码已注册");
			}else{
				$checkcodes = rand(100000, 999999) . '|' . time();//验证码
				$checkcode = explode("|", $checkcodes);
				$content = "您的短信验证码是:" . strtolower($checkcode[0]);
				$ok = _sendmobile($mobile, $content);
					
				if ($ok[0] != 1) {
					$this->failed("发送失败,失败状态:" . $ok[1]);
				} else {
			    $sql="INSERT INTO `@#_member`(username,mobile,img,mobilecode)VALUES('$mobile','$mobile','photo/member.jpg','$checkcodes')";
					$this->db->Query($sql);
					$info = array('state' => '发送成功', 'mobilecode' => $checkcode[0], 'sendtime' => $checkcode[1]);
					$this->success($info);
				
				}
			}
		}else{
			$this->failed("未知号码");
		}
	}

	
//注册
		public function register(){
				$name=isset($_POST['name']) ? $_POST['name'] : "";
				$userpassword=isset($_POST['userpassword']) ? $_POST['userpassword'] : "";
				$mobilecode = $_POST['mobilecode'];
				/*$name='13964271167';
				$userpassword=md5('111111');*/
				//$decode = intval($decode); 暂时不加邀请
				if(empty($name)||empty($userpassword)){
					$this->failed('用户名或者密码不能为空');
				}
				else {
					if ($mobilecode) {
						$checkcode = explode("|", $_SESSION['checkcode']);
						$times = time() - $checkcode[1];
						if ($times > 1200) {
							$this->failed('短信验证已过期');
						} else {
							$userpassword = $this->rsa->decrypt($userpassword);
							$user_ip = _get_ip_dizhi();
							$time = time();
							$logintoken = md5(time() . $userpassword);
							$sql = "UPDATE `@#_member` SET	`password` = '$userpassword',
													`user_ip`  = '$user_ip',
													 `emailcode` = '-1',
													 `mobilecode` = '1',
													 `reg_key` = '$name',
													 `time` = '$time',
													 `logintoken` = '$logintoken' WHERE `mobile`='$name'";

							$sql="INSERT INTO `@#_member`(username,password,mobile,img,emailcode,mobilecode,time,logintoken)VALUES('$name','$userpassword','$name','photo/member.jpg','-1','1','$time','$logintoken')";
							$sqlreg = $this->db->Query($sql);
							if ($sqlreg) {
								$member = $this->db->GetOne("select * from `@#_member` where `logintoken`='$logintoken'");
								$member["logintoken"] = $this->rsa->encrypt($logintoken);
								$member["token"] = $this->rsa->encrypt($logintoken);
								_setcookie("token", $logintoken, 3600);
								_setcookie("logintoken", $logintoken, 3600);
								$this->success(array("msg" => "注册成功", "userInfo" => $member));
							} else {
								$this->failed('注册失败');
							}
						}
					}else{
						$this->failed('验证码不能为空');
					}
				}
					

		}
		public function logout() {

					$_SESSION = array();
					session_unset();
					session_destroy();
					$_COOKIE = array();
					$this->success("已退出登录");
				}
	//修改密码获取验证码
	public function getChangePwdCode(){

		$phoneNumber = $_POST['mobile'];
		if($phoneNumber) {
			$member = $this->getUserInfoWihtMobile($phoneNumber);
			if($member) {
				$checkcodes = rand(100000, 999999) . '|' . time();//验证码
				$checkcode = explode("|", $checkcodes);
				$content = "你在" . _cfg("web_name") . "的短信验证码是:" . strtolower($checkcode[0]);
				$ok = _sendmobile($_POST['mobile'], $content);

				if ($ok[0] != 1) {
					$this->failed("发送失败,失败状态:" . $ok[1]);
				} else {
					$this->db->Query("UPDATE `@#_member` SET mobilecode='" . $checkcodes . "' where mobile='" . $phoneNumber . "'");
					$info = array('state' => '发送成功', 'mobilecode' => $checkcode[0], 'sendtime' => $checkcode[1]);
					$this->success($info);
				}
			}else{
				$this->failed("手机号码未注册");
			}
		}else{
			$this->failed("未知号码");
		}
	}
	//找回密码设置密码
	public function settingPwd(){
		if ($_POST['mobilecode']!='' && $_POST['newpassword']!='') {
			$mobilecode = $_POST['mobilecode'];
			$newpass = $this->rsa->decrypt($_POST['newpassword']);
			$mobile = $_POST['mobile'];
				if($mobilecode) {
					$member = $this->getUserInfoWihtMobile($mobile);
					$checkcode=explode("|",$member['mobilecode']);
					$times=time()-$checkcode[1];
//					$this->success(array('times'=>$times,'time'=>$checkcode[1]));
					if($times > 1200){
						$this->failed('短信验证已过期');
					}else {
						$res = $this->db->Query("UPDATE `@#_member` SET password='" . $newpass . "' where mobile='" . $mobile . "'");
						if ($res) {
							$this->success('密码修改成功');
						} else {
							$this->failed('密码修改失败');
						}
					}
			}else{
				$this->failed('旧密码验证错误');
			}
		}else{
			$this->failed("参数不完整");
		}
	}
	//修改密码
	public function changePwd(){
		if ($_POST['logintoken']!='' && $_POST['newpassword']!='') {
			$logintoken = $this->rsa->decrypt($_POST['logintoken']);
			$newpass = $this->rsa->decrypt($_POST['newpassword']);
			$oldpass = $this->rsa->decrypt($_POST['oldpassword']);

			$member = $this->getUserInfo($logintoken);
			$olddbpass = $member['password'];

//			$this->success(array("logintoken"=>$logintoken,"oldlocal"=>$oldpass,"serverpass"=>$olddbpass,"post"=>$_POST));
			if($olddbpass == $oldpass) {
				$res = $this->db->Query("UPDATE `@#_member` SET password='" . $newpass . "' where logintoken='" . $logintoken . "'");
				if ($res) {
					$this->success('密码修改成功');
				} else {
					$this->failed('密码修改失败');
				}
			}else{
				$this->failed('旧密码验证错误');
			}
		}else{
			$this->failed("参数不完整");
		}
	}
	//修改昵称
	public function changeNickName(){

		if ($_POST['logintoken']!='' && $_POST['newusername']!='') {
			$logintoken = $this->rsa->decrypt($_POST['logintoken']);
			$res=$this->db->Query("UPDATE `@#_member` SET username='".$_POST['newusername']."' where logintoken='".$logintoken."'");
			if ($res) {
				$this->success('用户名修改成功');
			}else{
				$this->failed('用户名修改失败');
			}
		}else{
			$this->failed("参数不完整");
		}
		
	}
	//修改手机发验证码
	public function changePhone(){
		/*$_POST['logintoken']='111111';
		$_POST['newmobile']='18669723768';*/
		if ($_POST['logintoken']!='' && $_POST['newmobile']!='') {
			$logintoken = $this->rsa->decrypt($_POST['logintoken']);
//			$member=$this->getUserInfo($logintoken);
			$isPhone=$this->db->GetOne("SELECT uid FROM `@#_member` WHERE `mobile` = '".$_POST['newmobile']."'LIMIT 1");
			if ($isPhone) {
				$this->failed('该手机号已被占用');
			}
			$checkcodes=rand(100000,999999).'|'.time();//验证码
			$checkcode=explode("|",$checkcodes);
			$content =  "你在"._cfg("web_name")."的短信验证码是:".strtolower($checkcode[0]);
			$ok=_sendmobile($_POST['newmobile'],$content);

			if($ok[0]!=1){
				$this->failed("发送失败,失败状态:".$ok[1]);
			}else{
			$this->db->Query("UPDATE `@#_member` SET mobilecode='".$checkcodes."' where logintoken='".$logintoken."'");
				$info = array('state' =>'发送成功','mobilecode'=>$checkcode[0],'sendtime'=>$checkcode[1]);
				$this->success($info);
			}
		}else{
			$this->failed("参数不完整");
		}
	}
	//验证手机
	public function mobilecheck(){
		/*$_POST['logintoken']='111111';
		$_POST['newmobile']='18669723768';
		$_POST['mobilecode']='605068';*/
		if ($_POST['logintoken']!='' && $_POST['newmobile']!='' && $_POST['mobilecode']!='') {
			$logintoken = $this->rsa->decrypt($_POST['logintoken']);
			$member=$this->getUserInfo($logintoken);
			$checkcode=explode("|",$member['mobilecode']);
			$times=time()-$checkcode[1];
			if($times > 1200){
				$this->failed('短信验证已过期');
			}else{
				if ($_POST['mobilecode']==$checkcode[0]) {
					$this->db->Query("UPDATE `@#_member` SET mobile='".$_POST['newmobile']."',mobilecode=1 where logintoken='".$logintoken."'");
					$this->success('修改手机成功');
				}else{
					$this->failed('修改手机失败');
				}
			}
			
		}
	}
	//修改邮箱
	public function changeEmail(){
		
	}

    //发送验证邮件
    public function sendsuccess(){
        if(!isset($_POST['email']) || empty($_POST['email']))$this->failed("邮箱地址不能为空!");
        if(!_checkemail($_POST['email'])) $this->failed("邮箱格式错误!");

        $config_email = System::load_sys_config("email");
        if(empty($config_email['user']) && empty($config_email['pass'])){
            $this->failed("邮箱系统故障,请联系管理员");
        }
        $uid = $_POST['uid'];
        $member=$this->getUserInfoWithUid($uid);
        $email = $_POST['email'];
        $member2=$this->db->GetOne("select * from `@#_member` where `email`='$email' and `uid` != '$uid'");
        if(!empty($member2) && $member2['emailcode'] == 1){
            $this->failed("该邮箱已经存在，请选择另外的邮箱验证！");
        }

        $strcode1=$email.",".$member['uid'].",".time();
        $strcode= _encrypt($strcode1);

        $tit=$this->_cfg['web_name_two']."激活注册邮箱";
        $content='<span>请在24小时内绑定邮箱</span>，点击链接：<a href="'.WEB_PATH.'/member/home/emailcheckingok/'.$strcode.'">';
        $content.=WEB_PATH.'/member/home/emailcheckingok/'.$strcode.'</a>';
        $succ=_sendemail($email,'',$tit,$content,'yes','no');
        if($succ=='no'){
           $this->failed("邮件发送失败!");
        }else{
           $this->success("邮件发送成功,请在24小时内到对应邮箱验证");
        }

    }
	//修改头像
	public function changeHeadPic(){
		if(!empty($_FILES)&&$_POST['logintoken']){
			$logintoken = $this->rsa->decrypt($_POST['logintoken']);
			if (!$this->getUserInfo($logintoken)) {
				$this->failed('用户不存在');
			}		
		
			System::load_sys_class('upload','sys','no');
			upload::upload_config(array('png','jpg','jpeg'),500000,'touimg');
			upload::go_upload($_FILES['file'],true);
			$files=time();
			if(!upload::$ok){
					$this->failed(upload::$error);
			}else{
				$img=upload::$filedir."/".upload::$filename;
				$size=getimagesize(G_UPLOAD."/touimg/".$img);
				$max=300;$w=$size[0];$h=$size[1];
				if($w>300 or $h>300){
					if($w>$h){
						$w2=$max;
						$h2 = intval($h*($max/$w));
						$point = array("x"=>0,"y"=>0,"w"=>'$w2',"h"=>'$h2');
						upload::thumbs($w2,$h2,true);
					}else{
						$h2=$max;
						$w2 = intval($w*($max/$h));
						$point = array("x"=>0,"y"=>0,"w"=>'$w2',"h"=>'$h2');
						upload::thumbs($w2,$h2,true);
					}
				}
				$tname="touimg/".$img;
			System::load_sys_class('upload','sys','no');
			upload::thumbs(160,160,false,G_UPLOAD.$tname,$point);
			upload::thumbs(80,80,false,G_UPLOAD.$tname,$point);
			upload::thumbs(30,30,false,G_UPLOAD.$tname,$point);
			$this->db->Query("UPDATE `@#_member` SET img='$tname' where logintoken='$logintoken'");

			$this->success($tname);
			}
		}

		
	}
	//获奖记录 需要传当前用户和当前p 页数 
		public function getResList(){
			$uid = $_POST['uid'];
//			$token = $this->rsa->decrypt($_POST['token']);
//			$time = $_POST['time'];
//			$trustToken = md5(_getcookie("token").$time);
//			if($token == $trustToken && $uid){
			$total=$this->db->GetCount("select * from `@#_member_go_record` where `uid`='$uid' and `huode`>'10000000' GROUP BY `shopid`");
			$page=System::load_sys_class('page');
			if(isset($_POST['page'])){$pagenum=$_POST['page'];}else{$pagenum=1;}
			$page->config($total,10,$pagenum,"0");
			$record = $this->db->GetPage("SELECT * FROM ((SELECT `shopid` as `id`,SUM(`moneycount`) as `q_buy_count` FROM `@#_member_go_record`  where uid = $uid and shopid in
				(SELECT `shopid` FROM `@#_member_go_record` where uid = $uid and `huode`>'10000000' group by `shopid`) group by `shopid` ORDER BY `shopid` DESC) as A )INNER JOIN(SELECT `thumb`,`qishu`,`title`,`zongrenshu`,`q_user_code`,`q_user`,`q_end_time`,`id`,`q_content`,`q_counttime`,`money`  FROM `@#_shoplist` WHERE  id in(SELECT `shopid` FROM `@#_member_go_record`  where uid = $uid group by `shopid`) order by `id` desc)  as B
		    ON A.id = B.id",array("num"=>10,"page"=>$pagenum,"type"=>1,"cache"=>0));
			$record = $this->unserializeArray($record);
			$isNoData = false;
			if($pagenum >= ($total / 10))
			{
				$isNoData = true;
			}
			if ($record) {
				$this->success(array("info"=>$record,"isNoData"=>$isNoData));
			}else{
				$this->failed('中奖记录为空！');
			}
//		}
//			else{
//				$this->failed('无权访问');
//			}
		}
		//用户购买记录  需要传当前用户和当前p 页数
	public function userBuyList(){
			$uid = $_POST['uid'];
			$state = $_POST['state'] ? $_POST['state'] : "0";
			$token = $this->rsa->decrypt($_POST['token']);
			$time = $_POST['time'];
			$trustToken = md5(_getcookie("token").$time);
			if($token == $trustToken && $uid) {
			$selecter = "";
			switch($state) {
				case "0": {
					//所有
					$selecter = "";
				}
					break;
				case  "1": {
					//进行中
                    $time = time();
					$selecter = "and `q_end_time` is null or (`q_end_time` is not null and `q_end_time` > $time)";
				}
					break;
				case "2": {
					//已揭晓
                    $time = time();
					$selecter = "and `q_end_time` is not null and `q_end_time` < $time";
				}
					break;
			}
				$record = $this->db->GetList("select `shopid` from `@#_member_go_record` where `uid`='$uid' and `status` != '未付款,未发货,未完成' group by `shopid` order by `id` DESC");
				$id = "";
		if($record) {
			foreach ($record as $key => $value) {
				$id .= intval($value['shopid']) . ',';
			}
			$id = str_replace(',0', '', $id);
			$id = trim($id, ',');
			$total = $this->db->GetCount("SELECT `sid` FROM `@#_shoplist` WHERE `id`IN ($id)$selecter");
			if ($total < 1) {
				$this->failed("购买记录为空!");
			}
			$page = System::load_sys_class('page');
			if (isset($_POST['page'])) {
				$pagenum = $_POST['page'];
			} else {
				$pagenum = 1;
			}
			$isNoData = false;
			if($pagenum >= $total/10)
			{
				$isNoData = true;
			}
			$page->config($total, 10, $pagenum, "0");
			$record = $this->db->GetPage("SELECT `thumb`,`q_end_time`,`qishu`,`title`,`zongrenshu`,`shenyurenshu`,`q_user`,`sid`,`q_user_code`,`id` FROM `@#_shoplist` WHERE `id` IN ($id) $selecter", array("num" => 10, "page" => $pagenum, "type" => 1, "cache" => 0));
			$record = $this->unserializeArray($record);
			if ($record) {
				$this->success(array("isNoData"=>$isNoData,"record"=>$record));
			} else {
				$this->failed('购买记录为空！');
			}
		}else{
			$this->failed('购买记录为空！');
		}
			}else{
				$this->failed("无权访问");
			}
		}
		public function unserializeArray($array){
			foreach ($array as $key => $value) {
				if(array_key_exists('info',$value))			{$array[$key]['info']=unserialize($value['info']);}
				if(array_key_exists('q_user',$value))		{$array[$key]['q_user']=unserialize($value['q_user']);}
				if(array_key_exists('picarr',$value))		{$array[$key]['picarr']=unserialize($value['picarr']);}
				if(array_key_exists('q_content',$value))	{$array[$key]['q_content']=unserialize($value['q_content']);}
			}
			return $array;
		}
		   //我的晒单列表
    public function singlelist(){
        $member=$this->userinfo;
        $title="我的晒单";
        $cord=$this->db->Getlist("select a.* from `@#_member_go_record` as a left join`@#_shoplist` as b on a.shopid=b.id where a.`uid`='$member[uid]' and a.`huode` > '10000000' and b.q_showtime='N'");

        $shaidan=$this->db->Getlist("select * from `@#_shaidan` where `sd_userid`='$member[uid]' order by `sd_id`");
        $sd_id = $r_id = array();
        foreach($shaidan as $sd){
            if(!empty($sd['sd_shopid'])){
                $sd_id[]=$sd['sd_shopid'];
            }
        }
        foreach($cord as $rd){
            if(!in_array($rd['shopid'],$sd_id)){
                $r_id[]=$rd['shopid'];
            }
        }
        if(!empty($r_id)){
            $rd_id=implode(",",$r_id);
            $rd_id = trim($rd_id,',');
        }else{
            $rd_id="0";
        }
        $record = $this->db->Getlist("select a.shopid,a.id from `@#_member_go_record` as a left join `@#_shoplist` as b on a.shopid=b.id where a.shopid in ($rd_id) and a.`uid`='$member[uid]' and a.`huode`>'10000000' and b.q_showtime='N'");
        $record_jf = $this->db->Getlist("select a.shopid,a.id from `@#_member_go_jf_record` as a left join `@#_jf_shoplist` as b on a.shopid=b.id where a.share=0 and a.`uid`='$member[uid]'");

        $total=$this->db->GetCount("select * from `@#_shaidan` where `sd_userid`='$member[uid]' order by `sd_id`");
        if(isset($_GET['p'])){$pagenum=$_GET['p'];}else{$pagenum=1;}
        $page=System::load_sys_class('page');
        $page->config($total,10,$pagenum,"0");
        $shaidan=$this->db->GetPage("select * from `@#_shaidan` where `sd_userid`='$member[uid]' order by `sd_id`",array("num"=>10,"page"=>$pagenum,"type"=>1,"cache"=>0));
        if($shaidan){
            $this->success(json_encode($shaidan));
        }else{
            $this->failed("暂无晒单记录");
        }

    }
    /*添加晒单*/
    public function singleinsert(){
        $member=$this->userinfo;
       // $uid=_getcookie('uid');
       // $ushell=_getcookie('ushell');
       // $title="添加晒单";
        $recordid=$_POST['recordid'];//获取订单id
       // $recordid=intval($this->segment(4));//获取订单id
        //$shopid = $recordid;
        //查询是否可以晒单
        $shaidan=$this->db->GetOne("select * from `@#_member_go_record` where `id`='$recordid' and `uid` = '$member[uid]'");
        if(!$shaidan){
            $this->failed("该商品您不可晒单!");

        }
        //查询是否晒单了
        $shaidanyn=$this->db->GetOne("select sd_id from `@#_shaidan` where `sd_shopid`='$recordid' and `sd_userid` = '$member[uid]'");
        if($shaidanyn){
            $this->failed("该订单已经添加晒单，不可重复添加晒单!");
        }

        //查询商品是否存在
        $ginfo=$this->db->GetOne("select id,sid,qishu,is_share from `@#_shoplist` where `id`='$shaidan[shopid]' LIMIT 1");
        if(!$ginfo){
            $this->failed("该商品已不存在!");
        }
        if($ginfo['is_share']!='1'){
            $this->failed("对不起，该商品不能晒单!");
        }
       /* $_POST['submit'] 获取app传来的信息*/
        if(isset($_POST['submit'])){
            if($_POST['title']==null)_message("标题不能为空");//晒单的标题
            if($_POST['content']==null)_message("内容不能为空");//晒单内容
           /* $_POST['fileurl_tmp'] 获取app传来的 图片*/
            if(!isset($_POST['fileurl_tmp'])){

                $this->failed("图片不能为空");
            }
            System::load_sys_class('upload','sys','no');
            $img=$_POST['fileurl_tmp'];
            $num=count($img);
            $pic="";
            for($i=0;$i<$num;$i++){
                $pic.=trim($img[$i]).";";
            }
            $src=trim($img[0]);

            if(!file_exists(G_UPLOAD.$src)){
                $this->failed("晒单图片不正确");
            }
            $size=getimagesize(G_UPLOAD.$src);
            $width=220;
            $height=$size[1]*($width/$size[0]);

            $src_houzhui = upload::thumbs($width,$height,false,G_UPLOAD.'/'.$src);
            $thumbs=$src."_".intval($width).intval($height).".".$src_houzhui;

            $sd_userid = $this->userinfo['uid'];
            $sd_shopid = $ginfo['id'];
            $sd_shopsid = $ginfo['sid'];
            $sd_qishu = $ginfo['qishu'];
            $sd_title = _htmtocode($_POST['title']);
            $sd_thumbs = $thumbs;
            $xss = new XssHtml($_POST['content']);
            $sd_content = $xss->getHtml();
            $sd_photolist= $pic;
            $sd_time=time();
            $sd_ip = _get_ip_dizhi();
            $result=$this->db->Query("INSERT INTO `@#_shaidan`(`sd_userid`,`sd_shopid`,`sd_shopsid`,`sd_qishu`,`sd_ip`,`sd_title`,`sd_thumbs`,`sd_content`,`sd_photolist`,`sd_time`)VALUES
			('$sd_userid','$sd_shopid','$sd_shopsid','$sd_qishu','$sd_ip','$sd_title','$sd_thumbs','$sd_content','$sd_photolist','$sd_time')");
            if($result){
                $this->success("添加晒单成功");
            }else{
                $this->failed("添加晒单失败");
            }

        }else{
            $this->failed("添加晒单失败");
        }

    }
	public function itemDetail()
		{
			$id = $_POST['id'];
			$uid = $_POST['uid'];
			$token = $this->rsa->decrypt($_POST['token']);
			$time = $_POST['time'];
			$trustToken = md5(_getcookie("token").$time);
		//	if($token == $trustToken && $uid) {
				if ($uid && $id) {
					$record = $this->db->GetList("SELECT `goucode`,`time` FROM `@#_member_go_record` WHERE `uid`=$uid AND `shopid`=$id and `goucode` != '' ORDER BY `time` DESC ");
					$itemInfo = $this->db->GetOne("SELECT * FROM `@#_shoplist` WHERE `id` = $id");
					$itemInfo['q_user'] = unserialize($itemInfo['q_user']);
					$itemInfo['picarr'] = unserialize($itemInfo['picarr']);
					$itemInfo['q_content'] = unserialize($itemInfo['q_content']);
					$returnData = array();
					if ($itemInfo) {
						$returnData['item'] = $itemInfo;
					} else {
						$returnData['item'] = "0";
					}
					if ($record) {
						$returnData['record'] = $record;
					} else {
						$returnData['record'] = "0";
					}

					if (!$itemInfo) {
						$this->failed("无效ID");
					} else {
						$this->success($returnData);
					}
				}
		//	}else{
		//		$this->failed('无权访问');
		//	}
		}
		public function memberAddress(){
			$uid = $_POST['uid'];
	//			$token = $this->rsa->decrypt($_POST['token']);
	//			$time = $_POST['time'];
	//			$trustToken = md5(_getcookie("token").$time);
	//		$logintoken = $this->rsa->decrypt($_POST['logintoken']);
	//			if($token == $trustToken && $uid){
			if($uid){
				$addressList = $this->db->GetList("SELECT * FROM `@#_member_dizhi` WHERE uid=$uid ORDER BY `default` DESC");
				if($addressList){
					$this->success($addressList);
				}else{
					$this->failed("无地址");
				}
			}

			//	}else{
			//		$this->failed('无权访问');
			//	}
		}
			//设置默认地址
		public function morenaddress(){

			$uid = $_POST['uid'];
				$token = $this->rsa->decrypt($_POST['token']);
				$time = $_POST['time'];
				$trustToken = md5(_getcookie("token").$time);
			    $logintoken = $this->rsa->decrypt($_POST['logintoken']);
				if($token == $trustToken && $uid){
			$member_dizhi=$this->db->Getlist("select * from `@#_member_dizhi` where uid='".$uid."'");
			foreach($member_dizhi as $dizhi){
				if($dizhi['default']=='Y'){
					$this->db->Query("UPDATE `@#_member_dizhi` SET `default`='N' where uid='".$uid."'");
				}
			}
			$id = $_POST['addressid'];
			if(isset($id)){
			$result = $this->db->Query("UPDATE `@#_member_dizhi` SET `default`='Y' where id='".$id."'");
			if ($result) {
					$this->success("设置成功");
				}else{
					$this->failed("设置失败");
				}		
			}
				}else{
					$this->failed('无权访问');
				}
		}
		//删除地址
		public function deladdress(){
			$uid=$_POST['uid'];
				$token = $this->rsa->decrypt($_POST['token']);
				$time = $_POST['time'];
				$trustToken = md5(_getcookie("token").$time);
			    $logintoken = $this->rsa->decrypt($_POST['logintoken']);
				if($token == $trustToken && $uid){
			$id = $_POST['addressid'];
			$dizhi=$this->db->Getone("select * from `@#_member_dizhi` where `uid`='$uid' and `id`='$id'");
			if(!empty($dizhi)){
				$this->db->Query("DELETE FROM `@#_member_dizhi` WHERE `uid`='$uid' and `id`='$id'");
				$this->success("删除成功");
			}else{
				$this->failed("删除失败");
			}
				}else{
					$this->failed('无权访问');
				}
		}
		//修改地址
		public function updateddress()
		{
			$uid = $_POST['uid'];
				$token = $this->rsa->decrypt($_POST['token']);
				$time = $_POST['time'];
				$trustToken = md5(_getcookie("token").$time);
			    $logintoken = $this->rsa->decrypt($_POST['logintoken']);
				if($token == $trustToken && $uid){

			if (isset($_POST['submit'])) {
				if (is_array($_POST['submit'])) {
					$submit = $_POST['submit'];
				} else {
					$submit = json_decode($_POST['submit']);
				}

				$province = isset($submit['sheng']) ? $submit['sheng'] : "";//省
				$city = isset($submit['shi']) ? $submit['shi'] : "";     //市
				$district = isset($submit['xian']) ? $submit['xian'] : "";//县 区
				$jiedao = isset($submit['jiedao']) ? $submit['jiedao'] : "";//街道
				$address = isset($submit['address']) ? $submit['address'] : "";//详细地址
				$shouhuoren = isset($submit['shouhuoren']) ? $submit['shouhuoren'] : "";//收货人
				$mobile = isset($submit['mobile']) ? $submit['mobile'] : "";//手机号
				$youbian = isset($submit['youbian']) ? $submit['youbian'] : "";//邮编
				$isDefault = isset($submit['isDefault']) ? $submit['isDefault'] : false;
				$id = $submit['id'];
				if($isDefault){
					$this->db->Query("UPDATE `@#_member_dizhi` SET `default` = 'N' WHERE `uid` = $uid AND `default` = 'Y'");
					$default = "Y";
				}else {
					$default = "N";
				}
				$result = $this->db->Query("UPDATE `@#_member_dizhi` SET
			`uid`='" . $uid . "',
			`sheng`='" . $province . "',
			`shi`='" . $city . "',
			`xian`='" . $district . "',
			`jiedao`='" . $jiedao . "',
			`youbian`='" . $youbian . "',
			`shouhuoren`='" . $shouhuoren . "',
			`address`='" . $address . "',
			`mobile`='" . $mobile . "',
			 `default`='".$default."'
			 where `id`='" . $id . "'");
				if ($result) {
					$this->success("地址修改成功");
				} else {
					$this->failed("地址修改失败");
				}
			}
				}else{
					$this->failed('无权访问');
				}
		}
		//添加地址
		public function addAddress()
		{
			$uid = $_POST['uid'];
					 	$token = $this->rsa->decrypt($_POST['token']);
						$time = $_POST['time'];
						$trustToken = md5(_getcookie("token").$time);
						if($token == $trustToken && $uid){

			if (isset($_POST['submit'])) {
				if (is_array($_POST['submit'])) {
					$submit = $_POST['submit'];
				} else {
					$submit = json_decode($_POST['submit']);
				}
				//			$this->success($submit);
				$province = isset($submit['sheng']) ? $submit['sheng'] : "";//省
				$city = isset($submit['shi']) ? $submit['shi'] : "";     //市
				$district = isset($submit['xian']) ? $submit['xian'] : "";//县 区
				$jiedao = isset($submit['jiedao']) ? $submit['jiedao'] : "";//街道
				$address = isset($submit['address']) ? $submit['address'] : "";//详细地址
				$shouhuoren = isset($submit['shouhuoren']) ? $submit['shouhuoren'] : "";//收货人
				$mobile = isset($submit['mobile']) ? $submit['mobile'] : "";//手机号
				$youbian = isset($submit['youbian']) ? $submit['youbian'] : "";//邮编
				$isDefault = isset($submit['isDefault']) ? $submit['isDefault'] : false;
				$time = time();
				$member_dizhi = $this->db->GetOne("select * from `@#_member_dizhi` where `uid`='" . $uid . "'");
				if (!$member_dizhi) {
					$default = "Y";
				} else {
					if ($isDefault) {
						$this->db->Query("UPDATE `@#_member_dizhi` SET `default` = 'N' WHERE `uid` = $uid AND `default` = 'Y'");
						$default = "Y";
					} else {
						$default = "N";
					}
				}
				$result = $this->db->Query("INSERT INTO `@#_member_dizhi`(`uid`,`sheng`,`shi`,`xian`,`jiedao`,`address`,`youbian`,`shouhuoren`,`mobile`,`default`,`time`)VALUES
				('$uid','$province','$city','$district','$jiedao','$address','$youbian','$shouhuoren','$mobile','$default','$time')");
				if ($result) {
					$this->success("地址添加成功");
				} else {
					$this->failed("地址添加失败");
				}

			}

				}else{
					$this->failed('无权访问');
				}
		}


	public function costRecord()
	{
		$uid = $_POST['uid'];
				 	$token = $this->rsa->decrypt($_POST['token']);
					$time = $_POST['time'];
					$trustToken = md5(_getcookie("token").$time);
					if($token == $trustToken && $uid){
		$countSql = "SELECT * FROM `@#_member_account` WHERE `uid` = '$uid' AND `type` != '1'  ORDER BY `time` DESC ";
		$total = $this->db->GetCount($countSql);
		$page = $_POST['page'] ? $_POST['page'] : 1;
		$configPage=System::load_sys_class('page');
		$configPage->config($total,10,$page,"0");
		$isNodata = fale;
		$maxPage = ceil($total / 10);
		if($page >= $maxPage){
			$isNodata = true;
		}

		$result = $this->db->GetPage($countSql,array("num"=>10,"page"=>$page,"type"=>1,"cache"=>0));

		if(!$result){
			$this->failed('无购买数据');
		}else{
			$this->success(array('isNoData'=>$isNodata,'info'=>$result));
		}

			}else{
				$this->failed('无权访问');
			}
	}
	public function chargeRecord()
	{
		$uid = $_POST['uid'];
				 	$token = $this->rsa->decrypt($_POST['token']);
					$time = $_POST['time'];
					$trustToken = md5(_getcookie("token").$time);
					if($token == $trustToken && $uid){
		$countSql = "SELECT * FROM `@#_member_account` WHERE `uid` = '$uid' AND `type` = '1' AND `content` LIKE '%充值%' ORDER BY `time` DESC ";
		$total = $this->db->GetCount($countSql);
		$page = $_POST['page'] ? $_POST['page'] : 1;
		$configPage=System::load_sys_class('page');
		$configPage->config($total,10,$page,"0");
		$isNodata = fale;
		$maxPage = ceil($total / 10);
		if($page >= $maxPage){
			$isNodata = true;
		}

		$result = $this->db->GetPage($countSql,array("num"=>10,"page"=>$page,"type"=>1,"cache"=>0));
		if(!$result){
			$this->failed('无购买数据');
		}else{
			$this->success(array('isNoData'=>$isNodata,'info'=>$result));
		}

			}else{
				$this->failed('无权访问');
			}
	}

}


?>